package cn.xaut.二叉树;

import cn.xaut.剑指offer.m20.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * 113. 路径总和 II
 */
public class demo113 {

    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<List<Integer>> path = new ArrayList<>();
        constructPath(root, new ArrayList<>(), path, targetSum);
        
        return path;
    }

    public void constructPath(TreeNode node, List<Integer> list, List<List<Integer>> path, int targetSum) {
        
        // 递归终止条件
        if (node == null)
            return;
        list.add(node.val);
        if (node.left == null && node.right == null && targetSum == node.val) {  // 如果是叶节点
            path.add(list);
            return;
        }

        // 递归过程
        constructPath(node.left, list, path, targetSum - node.val);
        constructPath(node.right, list, path, targetSum - node.val);
    }

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 5; i++)
            list.add(i);
        System.out.println(list);
        list.clear();
        System.out.println(list);
    }
}
